const formatTime = date => {
  var date = new Date(date);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatTime2 = date => {
  var date = new Date(date);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return year+"年"+month+"月"+day+"日"
}
const formatTime3 = (date,type) => {
  var date = new Date(date);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  if(type==2){
    return year+"年"+month+"月"
  }else{
    return year+"-"+month
  }
  
}
const Today = date => {
  var date = new Date(date);
  const day = date.getDate()
  return day
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const idCardNumber = n => {
    let idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    if (idcardReg.test(n)) {
        return true
    } else {
        return false
    }
}
function toHour(seconds,type){
  if (!seconds) return 0;
  seconds=Number(seconds)
  if(seconds<3600){
    var mins=(seconds/60).toFixed(2)?(seconds/60).toFixed(2):1
    type=type?type:'m'
    return mins + type
  }else{
    var hour=(seconds/60/60).toFixed(2)
    type=type?type:'h'
    return hour + type
  }
}

function randomNum(minNum, maxNum) {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10);
      break;
    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
      break;
    default:
      return 0;
      break;
  }
}


module.exports = {
  formatTime: formatTime,
  formatTime2: formatTime2,
  formatTime3,
  Today:Today,
  idCardNumber,
  toHour,
  randomNum
}
